import Sprite   from '../base/sprite'
import _COIGIG from '../runtime/mapdata'
import Beans    from '../runtime/beans'
import DataBus  from '../databus'

//const BULLET_IMG_SRC = 'images/bullet.png'
const PLAYER_WIDTH   = 20
const PLAYER_HEIGHT  = 20

const screenWidth  = window.innerWidth
const screenHeight = window.innerHeight

const MAP_OFFSETX=10 //x offset
const __ = {
  speed: Symbol('speed')
}

//const databus4._COS = [1,0,-1,0]
//const databus4._SIN = [0,1,0,-1]
	
let databus4 = new DataBus()
var lastclickmill=0

//两次单击小于300毫秒，可以认为是双击
    /*获取起始坐标和id*/
   

export default class Player extends Sprite {
  constructor(ctx,beans) {
    super("", PLAYER_WIDTH, PLAYER_HEIGHT)
	
	// 玩家默认处于屏幕底部居中位置
	
	this.coordx=13.5 //地图坐标
	this.coordy=23
    this.coordoffset=0
	
	this.coord={x:this.coordx,y:this.coordy}
	var pos = this.coord2position(this.coordx,this.coordy); //地图转画布
	this.x = pos.x //画布坐标
    this.y = pos.y
						
	this.width=20
	this.height=20
	
	this.orientation=2 //left
	
	this.leftwallflag=0
	this.rightwallflag=0
	this.upwallflag=0
	this.downwallflag=0
	
	
	this.startanimationx=-1;
	this.startanimationy=-1;
	
	this.animationend=false
	
	var myDate = new Date();
	this.curmill= myDate.getTime(); //毫秒数
	
	this.status=1 //1表示正常,3表示被吃
	this.times =0;
    lastclickmill=0
	
	this.timeout2=30
	this.speed=1.8//this[__.speed] //player speed (map speed)
	console.log(this.speed)
	//console.log(_COIGIG[0]['wall_color'])
	
	let t = this;
	
	this.enemys=null
	
  
  }

  setenemys(enemys){
    this.enemys=enemys
  }
  
  getstatus(){
	return this.status;
  }
	
  
  //移动方向判断
  _callorient(){
    if (this.status==3)
		return 
	
	/* 判断是否为同一次触摸，若不是则直接忽略*/
	
    if(this.endId === this.startId){
      let w = this.xt-this.x0
      let h = this.yt-this.y0
      let k = h/w
      /*不使用1判断斜率，而留有余量，防止误触*/
      if(k>2||k<-2){
        /*滑动20px以上激活，防止误触*/
        if(h<-20) {
		  console.log("up")	/*向上*/
		  this.orientation=3
		  this.downwallflag=0
		  this.leftwallflag=0
		  this.rightwallflag=0
        }
		if(h>20) {
		  console.log("down") /*向下*/
		  this.orientation=1
		  this.upwallflag=0
		  this.leftwallflag=0
		  this.rightwallflag=0
		  
        }
	  }
      if(k<0.5&&k>-0.5) {
        if(w<-20) {
		  //console.log("left") /*向左*/
		  this.orientation=2
		  this.rightwallflag=0
		  this.upwallflag=0
		  this.downwallflag=0
        }
		if(w>20) {
		  //console.log("right") /*向右*/
		  this.orientation=0
		   this.leftwallflag=0
		  this.upwallflag=0
		  this.downwallflag=0 
        }
	  }
    }
  }
  
  setbeans(beans){
	this.beans=beans
  }
  
  
  init(x, y, speed) {
    this.x = x
    this.y = y

    this[__.speed] = speed

    this.visible = true
  }

   
  update() {
  
      var myDate = new Date();
	  var nowmill= myDate.getTime(); //毫秒数
	
	  if (nowmill-this.curmill>=300){ //0.3 second玩家张口
	    this.times++;
	    this.curmill=nowmill;
		//databus4.MAP_OFFSETY=databus4.MAP_OFFSETY+1
	  }
	  
	  
	  if (typeof this.beans=='undefined')
		  return 
	  
	  if (this.status==3){
		  if (this.timeout2>0) //玩家被吃
	         this.timeout2=this.timeout2-1;
	  }
		
	  if (databus4.gamestart && this.status==1){
		 
	    
		    //转化为地图坐标
			if (!this.downwallflag && !this.upwallflag ){ //没有碰到墙壁
				
				var mappos= this.position2coord(this.x,this.y);
				//console.log("thisy:"+this.y)
				
				this.coordx=mappos.x;
				this.coordy=mappos.y;
				
			}
			
			var mapmoving=false
			
			//地图坐标，以1为单位(不需要speed)
			var value = this.get(this.coordx+databus4._COS[this.orientation],this.coordy+databus4._SIN[this.orientation]);
			var movingdirection=0;
			
			if (value!=1) {//没有碰墙壁，地图可以移动
				if (this.y<=60 && this.orientation==3 && databus4.MAP_OFFSETY<=10){ //向上到头部 (地图在移动，但是player的地图坐标不变，画布坐标在变)
				   console.log("cy:"+this.coordy)
				   if (this.coordy>=2 ){
					  console.log("orie:"+this.orientation)
					  databus4.MAP_OFFSETY=databus4.MAP_OFFSETY-this.speed*databus4._SIN[this.orientation]; //map speed
					
					  mapmoving=true
					  movingdirection=1;
				   }
				}
				
				if (Math.abs(this.y-screenHeight)<40 && this.orientation==1 && databus4.MAP_OFFSETY>=-170){ //向下到底部
				   console.log("cy:"+this.coordy)
				   if ( this.coordy<=this.y_length ){
					  console.log("orie:"+this.orientation)
					  databus4.MAP_OFFSETY=databus4.MAP_OFFSETY-this.speed*databus4._SIN[this.orientation];
					  mapmoving=true
					  movingdirection=-1;
				   }
				}
				
			}
			
		    this.coord={x:this.coordx,y:this.coordy} //保存更新后的地图坐标
			
			databus4.mapmoving=mapmoving
			databus4.movingdirection=movingdirection
			//if (databus4.mapmoving)
			 //  console.log("map moving");
			
			if(!this.beans.get(this.coordx,this.coordy)){	//吃豆
				 //_SCORE++;
				 
				 //console.log("吃豆"+"吃豆"+"吃豆"+"吃豆")
				 this.beans.set(this.coordx,this.coordy,1);
				 this.beans.ebnum=this.beans.ebnum+1
				 
				 if( _COIGIG[databus4.GAME_INDEX]['goods'][this.coordx+','+this.coordy]){	//吃到能量豆
					this.enemys.forEach(function(item){
						if(item.status==1||item.status==3){	//如果NPC为正常状态，则置为临时状态
							item.timeout = 450; //变成灰色的时间
							item.status = 3;
						}
					});
				 }
			}
			
			if(value==0){
				
				//解除某个方向上的墙壁障碍
				if (this.orientation==2)//left
				   this.rightwallflag=0
				else if (this.orientation==0) //right
				   this.leftwallflag=0;
				else if (this.orientation==1)//down
				   this.upwallflag=0
				else if (this.orientation==3) //up
				   this.downwallflag=0;
				
				this.animationend= false
				
				if (this.leftwallflag && this.orientation==2)  
					return ;
				if (this.rightwallflag && this.orientation==0)
                    return ;					
				
				
				this.x += this.speed*databus4._COS[this.orientation];
				
				
				if (this.downwallflag && this.orientation==1)
					return ;
				if (this.upwallflag && this.orientation==3)
					return ;
				
				if (mapmoving==false) //地图在动的时候，玩家无需移动，利用地图反响的移动会显得人物在动
				  this.y += this.speed*databus4._SIN[this.orientation];
			    
				
			}else if(value<0 ){ //左右穿墙
				this.x -= databus4.size*(this.x_length-1)*databus4._COS[this.orientation];
				this.y -= databus4.size*(this.y_length-1)*databus4._SIN[this.orientation];
				this.animationend= false
				
			}else{ //碰到墙壁 (value=1)
			     //to do  draw animation
				
			
				if (this.leftwallflag && !this.animationend ){
					if (this.x>this.startanimationx)
					  this.x -= this.speed
				    else
					  this.animationend=true
				  return 
	
				}
				else if (this.rightwallflag && !this.animationend ){
					if (this.x<this.startanimationx)
					  this.x += this.speed
				    else
					  this.animationend=true
				    return 
	
				}
				else if (this.upwallflag && !this.animationend ){
					if (this.y>this.startanimationy)
					  this.y -= this.speed
				    else
					  this.animationend=true
				  
				    return 
	
				}
				else if (this.downwallflag && !this.animationend){
					if (this.y<this.startanimationy)
					  this.y += this.speed
				    else
					  this.animationend=true
				  
				    return 
	
				}
				
				var offsetx=0
				if (this.orientation==2 ){ //left 
				   offsetx=Math.abs(this.x-databus4.MAP_OFFSETX-this.coordx*databus4.size)
				}else if (this.orientation==0 ){//right
				   offsetx=Math.abs(this.x-databus4.MAP_OFFSETX-(this.coordx+1)*databus4.size)
				}
				
				var offsety=0
				if (this.orientation==3 ){ //up //|| this.orientation==3
				   offsety=Math.abs(this.y-databus4.MAP_OFFSETY-this.coordy*databus4.size)
				}else if (this.orientation==1 ){ //down
				   offsety=Math.abs(this.y-databus4.MAP_OFFSETY-(this.coordy+1)*databus4.size)
				}
				
				if (!this.leftwallflag && this.orientation==2 ){ //离左墙壁还有较多空余 && this.coordx!=this.leftwallx
                   
				   if (offsetx>=10){
				     this.startanimationx=this.x-6
				     this.x -= this.speed
				     this.y += this.speed*databus4._SIN[this.orientation];
				     this.leftwallflag=1
				    
				   }
				   
				   
				   
				}else if (!this.rightwallflag && this.orientation==0 ){ //离右墙壁还有较多空余 && this.coordx!=this.rightwallx
                   //console.log("right")
				   if (offsetx>=10){
					   this.startanimationx=this.x+6
					   this.x += this.speed
					   
					   this.y += this.speed*databus4._SIN[this.orientation];
					   this.rightwallflag=1
					   
				   }
				  
				}else if (!this.upwallflag && this.orientation==3 ){ //离上墙壁还有较多空余 && this.coordy!=this.upwally
                   
				   if (offsety>=10){
					   this.x += this.speed*databus4._COS[this.orientation]
					   
					   this.startanimationy=this.y-6
					   this.y -= this.speed
					   this.upwallflag=1
					   
 					
					   
				   }
					  
				   
				}else if (!this.downwallflag && this.orientation==1){ //离下墙壁还有较多空余  && this.coordy!=this.downwally)
                   if (offsety>=10){
					   this.x += this.speed*databus4._COS[this.orientation]
					   this.startanimationy=this.y+6
					   this.y += this.speed
					   this.downwallflag=1
					   
					
					   
				   }
					   
				}
	        }
			
        }
		
		
		
	}
	  
  
  
  /**
   * 背景图画地图函数
   */
  render(context) {
    //if (databus4.gamestart){
		//console.log("draw player");
    	context.fillStyle = '#FFE600';
	    context.beginPath();
	    context.moveTo(this.x,this.y);
	    context.lineTo(this.x,this.y);
	   
		if(this.status!=3){	//玩家正常状态:status=1
		  if(this.times%2){
			 // console.log("draw player1");
 			 context.arc(this.x,this.y,this.width/2,(.5*this.orientation+.20)*Math.PI,(.5*this.orientation-.20)*Math.PI,false);
 		  }else{
			 // console.log("draw player2");
 			 context.arc(this.x,this.y,this.width/2,(.5*this.orientation+.01)*Math.PI,(.5*this.orientation-.01)*Math.PI,false);
 		  }
	    }else{	//玩家被吃
		   //if(stage.timeout) {
			  //this.timeout2 = 30;
			  context.arc(this.x,this.y,this.width/2,(.5*this.orientation+1-.02*this.timeout2)*Math.PI,(.5*this.orientation-1+.02*this.timeout2)*Math.PI,false);
		   //}
	    }
	  
	    
		context.closePath();
	    //context.stroke();
		context.fill();

	//}
  }
  
  
  
}
